﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;

public partial class Controls_Activity_FeedBackTable : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string activityIdString = Request["activityId"];
        if (!int.TryParse(activityIdString, out m_activityId))
            Response.Redirect("~/Home/Default.aspx");
        if (!ActivityRules.Instance.IsUserJoinedActivity(m_activityId, UserRules.Instance.GetCurrentUserId()))
            Response.Redirect("~/Home/Default.aspx");
        if (IsPostBack)
            return;
        List<string> questionList = ActivityRules.Instance.GetCustomFeedBackQuestions(m_activityId);
        if (questionList.Count != 0)
        {
            customQuestionPanel.Visible = true;
            grvCustomFeedBack.Visible = true;
            for (int i = 0; i < questionList.Count; i++)
            {
                customQuestion.Add(new CustomQuestion(i, questionList[i]));
            }
            grvCustomFeedBack.DataSource = customQuestion;
            grvCustomFeedBack.DataBind();
        }
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        string answer = "";
        foreach (GridViewRow r in grvCustomFeedBack.Rows)
        {
            TextBox answerBox = r.FindControl("txtAnswer") as TextBox;
            answer = answer + answerBox.Text + "#####";
        }
        int[] mark = { Convert.ToInt32(rbSatisfactionMark.SelectedValue), Convert.ToInt32(rbContentMark.SelectedValue), Convert.ToInt32(rbOrganizeMark.SelectedValue) };
        ActivityRules.Instance.UpdateFeedBackAnswer(m_activityId, UserRules.Instance.GetCurrentUserId(), answer, mark);
        Response.Redirect("~/Home/Default.aspx");
    }

    private List<CustomQuestion> customQuestion = new List<CustomQuestion>();
    private int m_activityId = -1;
}
class CustomQuestion
{
    public CustomQuestion(int id, string question)
    {
        ID = id;
        Question = question;
    }
    public int ID { get; set; }
    public string Question { get; set; }
}
